<?php
namespace app\blog\controller;

use app\blog\controller\Front;

class Type extends Front
{
    protected function initialize()
    {
        parent::initialize(); 
    }
    
    public function index()
    {
        //参数判断
        if( isset($this->query['id']) ){
            $data = model('blog/Type','loglic')->getId($this->query['id'], config('cache.expire_detail'), 'normal');
        }elseif( isset($this->query['slug']) ){
            $data = model('blog/Type','loglic')->getSlug($this->query['slug'], config('cache.expire_detail'), 'normal');
        }else{
            $data = [];
        }
        //数据判断
        if(!$data){
            abort(404);
        }
        //SEO标签
        $this->site['active'] = 'blogtypeindex'.$data['type_id'];
        $this->site['title'] = $this->seoReplace(xfEmpty($data['type_title'],$data['type_name']),true);
        $this->site['keywords'] = xfEmpty($data['type_keywords'],$data['type_name']);
        $this->site['description'] = $this->seoReplace(xfEmpty($data['type_description'],$data['type_excerpt']));
        //分页数据
        $data['type_limit'] = xfEmpty($data['type_limit'],config('channel.limit'));
        if($data['type_limit']){
            $pageList = $this->pageList($data['type_id'],$data['type_limit']);
        }
        //模板赋值
        $this->assign($data);
        $this->assign($pageList);
        $this->assign($this->site);
        //加载自定义模板
        if($data['type_tpl']){
            return $this->fetch($data['type_tpl']);
        }
        //默认加载
        return $this->fetch();
    }

    //分页查询
    private function pageList($typeId=0,$typeLimit=0)
    {
        $args = [
            'cache' => config('cache.expire_items'),
            'order' => [
                'detail_top' => 'desc',
                'detail_id'  => 'desc',
            ],
            'join'  => [
                ['type_map','type_map.type_mp_did = detail_id'],
            ],
            'where' => [
                ['type_map.type_mp_tid','eq',$typeId],
                ['detail_status','eq','normal'],
            ],
        ];
        //查询数据
        return model('blog/Index','loglic')->allPage($args, $typeLimit, $this->site['page'], 0, xfUrlPath('blog/type/index',$this->query));
    }
}